/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.transformation.editor.controller.editparts.link;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.ConnectionEndpointLocator;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.PolylineDecoration;
import org.eclipse.draw2d.RotatableDecoration;

import edu.pku.sei.modeler.edit.parts.AbstractLinkEditPart;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.model.Domain;
import edu.pku.sei.transformation.editor.model.DomainConnection;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.TypedModel;

/**
 * @generated
 * 
 */
public class DomainConnectionEditpart extends AbstractLinkEditPart {
	private RotatableDecoration targetDecor;
	// mode(check/enforce) tag
	private Label modeTag = new Label();
	private Label typeTag = new Label();

	public DomainConnectionEditpart(MetaShapeElement model) {
		super(model);
	}

	protected void createEditPolicies() {
		super.createEditPolicies();
	}

	private PolylineConnection getConnection() {
		return (PolylineConnection) getFigure();
	}

	@Override
	public void activate() {
		DomainConnection model = (DomainConnection) getLink().getModelElement();
		model.getDomain().addPropertyChangeListener(modelListener);
		super.activate();
	}

	@Override
	public void deactivate() {
		DomainConnection model = (DomainConnection) getLink().getModelElement();
		model.getDomain().removePropertyChangeListener(modelListener);
		super.deactivate();
	}

	protected IFigure createFigure() {
		DomainConnection dc = (DomainConnection) this.getLink()
				.getModelElement();
		PolylineConnection conn = new PolylineConnection();
		conn.setLineStyle(Graphics.LINE_DASH);
		ConnectionEndpointLocator modeLocator = new ConnectionEndpointLocator(
				conn, false);
		modeLocator.setVDistance(10);
		modeTag.setText("");
		conn.add(modeTag, modeLocator);

		ConnectionEndpointLocator typeLocator = new ConnectionEndpointLocator(
				conn, true);
		conn.add(typeTag, typeLocator);
		if (dc.getDomain().getTypedModel() != null)
			typeTag.setText(dc.getDomain().getTypedModel().getName());
		refreshModeTag();

		return conn;
	}

	private PolylineDecoration createDecoration() {
		PolylineDecoration decoration = new PolylineDecoration();
		decoration.setScale(10, 5);
		return decoration;
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		if (ModelPackage.DOMAIN_CHECKABLE.equals(propertyName)) {
			refreshModeTag();
		} else if (ModelPackage.DOMAIN_ENFORCEABLE.equals(propertyName)) {
			refreshModeTag();
		} else if (ModelPackage.DOMAIN_TYPEDMODEL.equals(propertyName)) {
			TypedModel param = (TypedModel) event.getNewValue();
			if (param == null) {
				typeTag.setText("");
			} else {
				typeTag.setText(param.getName());
			}
		}
	}

	private void refreshModeTag() {
		Domain domain = ((DomainConnection) this.getLink().getModelElement())
				.getDomain();
		if (domain.isEnforceable()) {
			this.modeTag.setText("Enforce");
		} else if (domain.isCheckable()) {
			this.modeTag.setText("Check");
		} else {
			this.modeTag.setText("");
		}
	}

	protected void refreshVisuals() {
		super.refreshVisuals();
		updateTargetDecoration();
	}

	private void updateTargetDecoration() {
		if (targetDecor == null) {
			targetDecor = createDecoration();
			getConnection().setTargetDecoration(targetDecor);
		}
	}
}
